<?php

class ViewVideo extends MY_Controller {

    public function __construct() {
        parent::__construct();
        
        if($this->session->userdata('user_type') != 1 || $this->session->userdata('user_type') == null){
            show_error("You have no permission here");
        }
    }

    function index() {
        
        $this->load->library('pagination');
        $this->load->library('table');


        $config['base_url'] = 'http://localhost/AsNew/ci/index.php/ViewVideo/index';
        $config['total_rows'] = $this->db->get('VIDEO')->num_rows();
        $config['per_page'] = 10;
        $config['num_links'] = 20;
        $config['full_tag_open'] = '<div id="pagination">';
        $config['full_tag_close'] = '</div>';

        $this->pagination->initialize($config);

        $this->load->model('Video_Model');
        $query = $this->Video_Model->getAllVideo($config['per_page'], $this->uri->segment(3));
        $result = $query->result();

        $this->table->set_heading('No', 'Video Name', 'Video Detail', 'Video Path', 'Manage'); //Create columns

        if ($query->num_rows() > 0) {
            for ($i = 0; $i < count($result); $i++):
                $row = $result[$i];
                $this->table->add_row(
                        $i + 1, $row->VIDEO_NAME, $row->VIDEO_DETAIL, $row->VIDEO_PATH, anchor(base_url() . 'index.php/ViewVideo/getVideo/' . $row->ID, '<img src="' . base_url() . 'images/view.png" alt="View" />')
                );
            endfor;
        }else {
            $cell = array('data' => 'No results found', 'colspan' => 5, 'align' => 'center');
            $this->table->add_row($cell);
        }

        $tmpl = array(
            'table_open' => '<table border="0" width="100%" cellpadding="0" cellspacing="0" id="product-table">',
            'heading_cell_start' => '<th class="table-header-repeat line-left"><a href>',
            'heading_cell_end' => '</a></th>',
            'table_close' => '</table>');

        $this->table->set_template($tmpl);

        $this->load->Model('Course_Model');
        $courseList = $this->Course_Model->getCourseList();
        $this->data['courseList'] = $courseList;
        
        $this->data['main_title'] = "View Instruction Media";
        $this->data['action'] = 'view';
        $this->data['main_content'] = 'video';
        $this->load->view('includes/template', $this->data);
    }

    function searchVideo() {
        $this->load->library('pagination');
        $this->load->library('table');


        $config['base_url'] = 'http://localhost/AsNew/ci/index.php/ViewVideo/index';
        $config['total_rows'] = $this->db->get('VIDEO')->num_rows();
        $config['per_page'] = 10;
        $config['num_links'] = 20;
        $config['full_tag_open'] = '<div id="pagination">';
        $config['full_tag_close'] = '</div>';

        $this->pagination->initialize($config);

        $search_course = $this->input->post('search_course');
        $search_videoname = $this->input->post('search_videoname');

        $this->load->model('Video_Model');
        $query = $this->Video_Model->searchVideo($search_course, $search_videoname, $config['per_page'], $this->uri->segment(3));
        $result = $query->result();

        $this->table->set_heading('No', 'Video Name', 'Video Detail', 'Video Path', 'Manage'); //Create columns

        if ($query->num_rows() > 0) {
            for ($i = 0; $i < count($result); $i++):
                $row = $result[$i];
                $this->table->add_row(
                        $i + 1, $row->VIDEO_NAME, $row->VIDEO_DETAIL, $row->VIDEO_PATH, anchor(base_url() . 'index.php/ViewVideo/getVideo/' . $row->ID, '<img src="' . base_url() . 'images/view.png" alt="View" />')
                );
            endfor;
        }else {
            $cell = array('data' => 'No results found', 'colspan' => 5, 'align' => 'center');
            $this->table->add_row($cell);
        }

        $tmpl = array(
            'table_open' => '<table border="0" width="100%" cellpadding="0" cellspacing="0" id="product-table">',
            'heading_cell_start' => '<th class="table-header-repeat line-left"><a href>',
            'heading_cell_end' => '</a></th>',
            'table_close' => '</table>');

        $this->table->set_template($tmpl);

        $this->load->Model('Course_Model');
        $courseList = $this->Course_Model->getCourseList();
        $this->data['courseList'] = $courseList;
        
        $this->data['main_title'] = "View Instruction Media";
        $this->data['action'] = 'view';
        $this->data['main_content'] = 'video';
        $this->load->view('includes/template', $this->data);
    }

    function getVideo($videoID) {
        $this->load->Model('Video_Model');
        $query = $this->Video_Model->getVideoByID($videoID);

        if ($query->num_rows() == 1) { // if the user's credentials validated...
            $row = $query->row();

            $video = array(
                'id' => $row->ID,
                'video_name' => $row->VIDEO_NAME,
                'course' => $row->COURSE_ID,
                'video_path' => $row->VIDEO_PATH,
                'video_detail' => $row->VIDEO_DETAIL,
            );
            $this->data['video'] = $video;
        }

        //get dropdownlist for course
        $this->load->Model('Course_Model');
        $courseList = $this->Course_Model->getCourseList();
        $this->data['courseList'] = $courseList;

        $course = 'class="styledselect-course" id="course" disabled="disabled"';

        $videoName = array(
            'disabled' => 'diabled',
            'name' => 'videoName'
        );

        $videoDetail = array(
            'disabled' => 'diabled',
            'name' => 'videoDetail'
        );
        $videoPath = array(
            'disabled' => 'diabled',
            'name' => '$videoPath'
        );

        $this->data['course'] = $course;
        $this->data['videoName'] = $videoName;
        $this->data['videoDetail'] = $videoDetail;
        $this->data['videoPath'] = $videoPath;
        
        $this->data['main_title'] = "View Instruction Media";
        $this->data['action'] = 'view';
        $this->data['main_content'] = 'add_video';
        $this->load->view('includes/template', $this->data);
    }

}

?>
